package com.kafka.kafkatest;

import com.github.houbb.heaven.util.lang.ByteUtil;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;

public class Ucs2ToString {
    public static void main(String[] args) throws CharacterCodingException {

        String code = "9767D36CBE8F200020002000200020002000200020002000200020002000310030003100310039003900300030003200300035007F5E1C4E0177" +
                "4696304E025E3275505B4795CE577F895167D4591A4F7F89E895578833003200F7534B4E8C4E200020002000200020002000200020002000200020002" +
                "00020003400340031003500380031003100390039003000300032003000350030003800310030004696304E025E6C51895B405C20002000200020002000" +
                "20002000200020003200300031003600300039003200360032003000330036003000390032003600200020002000200020002000200020002000200020002" +
                "000200020002000200020002000";

        String idCode=code.substring(0,60);
        // 假设这是UCS-2小端编码的字节数组
        byte[] ucs2Bytes = {0x20, 0x5F, 0x2D, 0x4E, 0x2A, (byte) 0x82,
                0x20,
                0x00,
                0x20,
                0x00,
                0x20,
                0x00,
                0x20,
                0x00,
                0x20,
                0x00,
                0x20,
                0x00,
                0x20,
                0x00,
                0x20,
                0x00,
                0x20,
                0x00,
                0x20,
                0x00,
                0x20,
                0x00,
                0x20,
                0x00};
        for (byte b:ucs2Bytes){
            System.out.println(b);
        }

//        System.out.println(20));

        // 创建一个ByteBuffer来包装字节数组
        ByteBuffer byteBuffer = ByteBuffer.wrap(ucs2Bytes);
        // 创建一个CharsetDecoder来解码UCS-2（使用UTF-16并指定小端字节序）
        CharsetDecoder decoder = StandardCharsets.UTF_16LE.newDecoder();
        // 解码字节流为字符
        CharBuffer charBuffer = decoder.decode(byteBuffer);
        // 将字符转换为字符串
        String decodedString = charBuffer.toString();
        // 打印解码后的字符串（注意：这可能包含无法打印的字符或乱码，取决于字节内容）
        System.out.println("Decoded String: " + decodedString);
        // 如果您确实需要将字符串转换为GB18030编码的字节数组（尽管通常不需要）
        // byte[] gb18030Bytes = decodedString.getBytes(Charset.forName("GB18030"));
    }
}
